package com.panan.GenTool;

import com.panan.Config;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class GenBaseMapper {

    public GenBaseMapper() {

        String content = parse();

        try {
            File directory = new File("");
            String path=this.getClass().getResource("").getPath();
            String outputPath = directory.getAbsolutePath()+ "/src/main/java/"+ Config.map.get("mapper.basemapper.location").replace(".", "/")+"/"+Config.map.get("mapper.basemapper.name")+".java";
            FileWriter fw = new FileWriter(outputPath);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(content);
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能：生成Mapper主体代码
     */
    private String parse() {
        StringBuffer sb = new StringBuffer();
        if("true".equalsIgnoreCase(Config.map.get("mapper.basemapper"))) {
            sb.append("package " + Config.map.get("mapper.basemapper.location") + ";\r\n\n");
            sb.append("import java.util.List;");
            sb.append("\r\n");
            //注释部分
            sb.append("/**\r\n");
            sb.append(" * " + Config.map.get("entity.mapper.name") + "类\r\n");
            sb.append(" * @version 1.0.0" + "\r\n");
            sb.append(" * @author " + Config.map.get("entity.annotation.author") + "\r\n");
            sb.append(" * @since " + new Date() + "\r\n");
            sb.append(" */ \r\n");
            //
            sb.append("public interface " + Config.map.get("mapper.basemapper.name")+"<T>"+"{\r\n\n");
            if("true".equalsIgnoreCase(Config.map.get("mapper.entity.method"))){
                sb.append("\tint "+Config.map.get("mapper.entity.insert")+"(T t);\r\n\n");
                sb.append("\tint "+Config.map.get("mapper.entity.delete")+"(T t);\r\n\n");
                sb.append("\tList<T> "+Config.map.get("mapper.entity.findall")+"();\r\n\n");
                sb.append("\tint "+Config.map.get("mapper.entity.update")+"(T t);\r\n\n");
            }else {
                sb.append("\tint save(T t);\r\n\n");
                sb.append("\tint delete(T t);\r\n\n");
                sb.append("\tList<T> findAll();\r\n\n");
                sb.append("\tint update(T t);\r\n\n");
            }
            sb.append("}\r\n");
        }else{

        }
        //System.out.println(sb.toString());
        return sb.toString();
    }

}
